#include "stdafx.h"

int (*FilesRefactorUtils::s_FrogMessageBoxFunc)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) = NULL;

int FilesRefactorUtils::FrogMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
	if (s_FrogMessageBoxFunc)
	{
		return s_FrogMessageBoxFunc(hWnd, lpText, lpCaption, uType);
	}
	else
	{
		return MessageBox(hWnd, lpText, lpCaption, uType);
	}
}

void FilesRefactorUtils::GetImageSize(HPDF_REAL* width, HPDF_REAL* height, const wstring& imgFile)
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR  gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	
	{
	Image image(imgFile.c_str());
	*width = (HPDF_REAL)image.GetWidth();
	*height = (HPDF_REAL)image.GetHeight();
	}

	GdiplusShutdown(gdiplusToken);
}

void FilesRefactorUtils::GetImageSizeInRect(HPDF_REAL *width, HPDF_REAL *height, HPDF_REAL rectW, HPDF_REAL rectH, const std::wstring &imgFile)
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR  gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	
	HPDF_REAL actW;
	HPDF_REAL actH;
	{
	Image image(imgFile.c_str());
	actW = (HPDF_REAL)image.GetWidth();
	actH = (HPDF_REAL)image.GetHeight();
	}

	if (actW > rectW)
	{
		float z = actW/rectW;
		actW /= z;
		actH /= z;
	}
	if (actH > rectH)
	{
		float z = actH/rectH;
		actW = actW / z;
		actH = actH / z;
	}

	*width = actW;
	*height = actH;

	GdiplusShutdown(gdiplusToken);	
}

int FilesRefactorUtils::GetEncoderCLSID(const WCHAR* format, CLSID* pClsid)
{
	unsigned int  num = 0;    // number of image encoders
	unsigned int  size = 0;   // size of the image encoder array in bytes

	GetImageEncodersSize(&num, &size);
	if(size == 0)return -1;

	ImageCodecInfo* imageCodecInfo = new Gdiplus::ImageCodecInfo[size];
	GetImageEncoders(num, size, imageCodecInfo);

	for(unsigned int i = 0; i < num; ++i)
	{
		if( wcscmp(imageCodecInfo[i].MimeType, format) == 0 )
		{
			*pClsid = imageCodecInfo[i].Clsid;
			delete[] imageCodecInfo;
			return i;
		}    
	}
	delete[] imageCodecInfo;
	return -1;
}

//void FilesRefactorUtils::OptimizeImage(HPDF_REAL curW, HPDF_REAL curH, const wstring& imgFile)
//{
//	HPDF_REAL width;
//	HPDF_REAL height;
//
//	GetImageSize(&width, &height, imgFile);
//
//	if ((width > curW) && (height > curH))
//	{
//		GdiplusStartupInput gdiplusStartupInput;
//		ULONG_PTR  gdiplusToken;
//		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//
//		{
//		Image image(imgFile.c_str());
//		image.
//		}
//
//		GdiplusShutdown(gdiplusToken);
//	}
//}

DWORD FilesRefactorUtils::Touch(LPCTSTR lpszFile, FILETIME* atime, FILETIME* mtime, FILETIME* ctime)
{
	SetLastError(ERROR_SUCCESS);

	HANDLE hFile = CreateFile(
		lpszFile, 
		GENERIC_WRITE, 
		FILE_SHARE_READ | FILE_SHARE_WRITE, 
		NULL, 
		OPEN_EXISTING, 
		FILE_ATTRIBUTE_NORMAL, 
		0);

	DWORD dwRetVal = GetLastError();

	// Check for CreateFile() special cases
	if (dwRetVal != ERROR_SUCCESS)
	{
		if (dwRetVal == ERROR_FILE_NOT_FOUND)
		{
			return ERROR_SUCCESS; // not an error
		}
		else if (hFile != INVALID_HANDLE_VALUE && dwRetVal == ERROR_ALREADY_EXISTS)
		{
			dwRetVal = ERROR_SUCCESS; // not an error according to MSDN docs
		}
	}

	if (dwRetVal != ERROR_SUCCESS)
	{
		return dwRetVal;
	}

	// Is there any template timestamp?  
	if (atime || mtime || ctime)
	{
		SetLastError(ERROR_SUCCESS);

		SetFileTime(hFile, ctime, atime, mtime);

		dwRetVal = GetLastError();
	}

	CloseHandle(hFile);

	return dwRetVal;
}


